<?php
class ulog extends Model{
	

	/**
	 * 获取前台用户日志
	 * @param unknown_type $page
	 * @param unknown_type $num
	 * @param unknown_type $usertype
	 * @param unknown_type $username
	 * @param unknown_type $start
	 * @param unknown_type $end
	 * @return multitype:unknown number Ambigous <boolean, multitype:>
	 */
	public function getulog($page =1 , $num = 10 ,$usertype = 0 , $username = NULL , $start = null,$end =null){
		
		$sql = "SELECT `ulog`.*, `uaction`.*, `uuser`.* FROM `ulog` 
				LEFT JOIN `uaction` ON `uaction`.`uaction_id` = `ulog`.`uaction_id`
				LEFT JOIN `uuser` ON `uuser`.`user_id` = `ulog`.`user_id` ";
		if($usertype == 1 || $usertype == 0){//筛选教师，学生
			$where = " WHERE `uaction`.`uaction_type` =" .$usertype;
		}
		else{
			$where = " WHERE 1";
		}
		if($username != "null"){//名字筛选
			$where .=" AND `uuser`.`user_nickname` LIKE '%".$username."%' ";
		}
		if($start){//开始结束时间
			$where .= " AND `ulog`.`ulog_create_time` > '".$start."' ";
		}
		if($end){
			$where .= " AND `ulog`.`ulog_create_time` < '".$end."' ";
		}
		$limit = " ORDER BY `ulog`.`ulog_create_time` DESC 
				   Limit ".( $page - 1 ) * $num.",".$num." ";
		//echo $sql.$where.$limit;
		$list = $this->fetchAll($sql .$where .$limit );
		$total = count($this->fetchAll($sql .$where ));
		$totalPage = ceil($total / $num);
		return array('page'=>$page,'list'=>$list,'total'=>$total,'totalPage'=>$totalPage);
	}
	
	
	

	public function addUlog($userid, $actionid, $flag) {
// 		echo 1;
		$sql = "SELECT uaction_name FROM uaction WHERE uaction_id = $actionid";
		$ret = $this->fetchRow($sql);
		if ($flag) {
			$t = "成功";
		} else {
			$t = "失败";
		}
		$sql = "INSERT INTO ulog VALUES (null, $userid, NOW(), $actionid, $flag, '".$ret['uaction_name']."$t')";
// 		echo $sql;
		$this->insert($sql);
	}

	
}